package se.lnu.axez;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.CharBuffer;
import java.nio.FloatBuffer;
import java.nio.ShortBuffer;

/**
 * Utility class for buffers creation.
 * @author stan
 *
 */
final class Buffers {
	public static final int BITS_IN_BYTE = 8;
	public static final int CHAR_BYTES = Short.SIZE / BITS_IN_BYTE; // char size is equal to short size in bytes
	public static final int FLOAT_BYTES = Float.SIZE / BITS_IN_BYTE;

	/**
	 * This class cannot be instantiated.
	 */
	private Buffers() { }
	
	/**
	 * Allocates {@link FloatBuffer} of specified length.
	 * 
	 * @param length
	 *            - length of the buffer to be allocated.
	 * @return - newly allocated buffer.
	 */
	public static FloatBuffer allocateFloat(int length){
		return  ByteBuffer
				.allocateDirect(length * 4)
				.order(ByteOrder.nativeOrder())
				.asFloatBuffer();
	}
	
	/**
	 * Allocates {@link ShortBuffer} of specified length.
	 * 
	 * @param length
	 *            - length of the buffer to be allocated.
	 * @return - newly allocated buffer.
	 */
	public static ShortBuffer allocateShort(int length){
		return  ByteBuffer
				.allocateDirect(length * 2)
				.order(ByteOrder.nativeOrder())
				.asShortBuffer();
	}
	
	/**
	 * Allocates {@link CharBuffer} of specified length.
	 * 
	 * @param length
	 *            - length of the buffer to be allocated.
	 * @return - newly allocated buffer.
	 */
	public static CharBuffer allocateChar(int length){
		return  ByteBuffer
				.allocateDirect(length * 2)
				.order(ByteOrder.nativeOrder())
				.asCharBuffer();
	}

	/**
	 * Allocates {@link ByteBuffer} of specified length.
	 * 
	 * @param length
	 *            - length of the buffer to be allocated.
	 * @return - newly allocated buffer.
	 */
	public static ByteBuffer allocateByte(int length){
		return  ByteBuffer
				.allocateDirect(length)
				.order(ByteOrder.nativeOrder());
	}
}
